package com.studio49.exmple.controller.web.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.util.PubDateUtils;
import com.studio49.exmple.core.controller.BaseController;
import com.studio49.exmple.entity.web.sys.Menus;
import com.studio49.exmple.entity.web.sys.RoleMenusRel;
import com.studio49.exmple.mapper.web.sys.MenuMapper;
import com.studio49.exmple.mapper.web.sys.RoleMenusRelMapper;
import com.studio49.exmple.service.web.sys.MenusService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {
	
	@Autowired
	private MenuMapper menuMapper;
	
	@Autowired
	private MenusService menusService;
	
	@Autowired
	private RoleMenusRelMapper roleMenusRelMapper;
	
	/**
	 * 列表页
	 * @return
	 */
	@RequestMapping("/list")
	public String list(){
		return ForwardConstant.SYSTEM_H + ForwardConstant.ROLE + ForwardConstant.LIST;
	}
	
	/**
	 * 根据菜单key，查询菜单
	 * @param menuKey
	 * @return
	 */
	@RequestMapping("/findMenuByKey")
	@ResponseBody
	public Menus findMenuByKey(String menuKey){
		Menus menus = new Menus();
		menus.setMenuKey(menuKey);
		menus.setIsOpen((short) 1);
		menus.setIsDeleted((short) 0);
		
		List<Menus> list = menuMapper.select(menus);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return new Menus();
	}
	
	 /**
     * 权限分配页面
     * 
     * @param model
     * @return
     */
    @RequestMapping("menuRightUI")
    public String menuRightUI(Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return ForwardConstant.SYSTEM_H + ForwardConstant.MENU + ForwardConstant.MENU_RIGHT;
    }
    
    /**
     * 给角色添加菜单权限
     * 
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("addMenuRight")
    @Transactional
    public String addMenuRight(Long roleId, String ids) throws Exception {
    	
    	RoleMenusRel roleMenusRel = new RoleMenusRel();
    	roleMenusRel.setRoleId(roleId);
    	roleMenusRelMapper.delete(roleMenusRel);
    	
    	Date now = PubDateUtils.getNow();
    	if(StringUtils.isNotBlank(ids)) {
    		for(String id : ids.split(",")) {
    			RoleMenusRel add = new RoleMenusRel();
    			add.setRoleId(roleId);
    			add.setMenuId(Long.valueOf(id));
    			add.setCreateTime(now);
    			add.setModifyTime(now);
    			roleMenusRelMapper.insert(add);
    		}
    	}
    	
        return SysConstant.SUCCESS;
    }

    /**
     * 获取角色菜单信息
     * 
     * @return
     */
    @ResponseBody
    @RequestMapping("findRoleMenu")
    public List<Menus> findUserRes(Long roleId) {
    	List<Menus> list = menuMapper.findByRoleIds(roleId + "");
    	if(CollectionUtils.isEmpty(list)) {
    		return new ArrayList<>();
    	}
        return list;
    }
    
    /**
     * 获取所有菜单信息
     * 
     * @return
     */
    @ResponseBody
    @RequestMapping("findAllMenu")
    public List<Menus> findAllMenu() {
    	List<Menus> list = menusService.findAll();
    	if(CollectionUtils.isEmpty(list)) {
    		return new ArrayList<>();
    	}
        return list;
    }
    
    
}
